<!-- 组件说明 -->
<template>
  <div class="loginwrap">
    <goback :title="''"></goback>
    <div class="title">
      Enter Verification Code
    </div>
    <p class="tips">Verification code has been sent to +{{ phoneShow }}</p>
    <div class="phoneNumberShowWrap">
      <van-field
        class="phoneNumberShow"
        readonly
        clickable
        :value="phoneCode"
        @touchstart.native.stop="keyboardShow = true"
      />
      <div class="codeLiWrap" @click.stop="keyboardShow = true">
        <div
          class="codeLi"
          v-for="(item, index) in phoneCodeLiShow"
          :key="index"
        >
          {{ item }}
        </div>
      </div>
    </div>
    <van-number-keyboard
      v-model="phoneCode"
      :show="keyboardShow"
      :maxlength="6"
      @blur="keyboardShow = false"
    />
    <p class="tips" v-if="lastTime != initLastTime">
      Resend after {{ lastTime + "S" }}
    </p>
    <p
      class="tips tipsBtn"
      @click.stop="resendFn"
      v-if="lastTime == initLastTime"
    >
      Resend again
    </p>

    <div class="phoneNumberShowWrap" v-if="ifNew == 1">
      <van-field
        class="phoneNumberShow"
        readonly
        clickable
        :value="inviteCode"
        @touchstart.native.stop="keyboardShow2 = true"
      />
    </div>
    <van-number-keyboard
      v-model="inviteCode"
      :show="keyboardShow2"
      :maxlength="20"
      @blur="keyboardShow2 = false"
    />

    <div class="btns">
      <van-button
        :class="btnReady ? 'loginBtn loginBtnReady' : 'loginBtn'"
        @click.stop="loginBtnFn"
        :loading="loginIfLoading"
        type="info"
        loading-text="loading..."
        >Login</van-button
      >
    </div>
  </div>
</template>

<script>
//import x from ''
export default {
  components: {},
  data() {
    return {
      phoneShow: "",
      phoneNumber: "",
      lastTime: 0,
      initLastTime: 60,
      timeInterval: null,
      keyboardShow: false,
      phoneCode: "",
      loginIfLoading: false,
      inviteCode: "", //首次登录的邀请码
      ifNew: false,
      keyboardShow2: false
    };
  },
  watch: {
    phoneCodeLiShow(newVal, oldVal) {
      console.log(newVal);
    }
  },
  computed: {
    btnReady() {
      if (this.phoneCode && this.phoneCode.length == 6) {
        return true;
      }
      return false;
    },
    phoneCodeLiShow() {
      let phoneCode = this.phoneCode.toString();
      console.log(phoneCode);
      if (phoneCode) {
        let targetDataVal = phoneCode.split("");
        let currentLength = targetDataVal.length;
        for (let i = 0; i < 6 - currentLength; i++) {
          targetDataVal.push("");
        }
        return targetDataVal;
      } else {
        return ["", "", "", "", "", ""];
      }
    }
  },
  methods: {
    resendFn() {
      let zs = this;
      this.$toast.loading({
        message: "waiting",
        duration: 0,
        forbidClick: true
      });
      zs.api.userController
        .userObatinRegistersms({
          phone: zs.phoneNumber
        })
        .then(res => {
          console.log(res);
          if (res.data.code === 200) {
            zs.$toast("send code SUCCESS");
            this.lastShow();
          } else {
            zs.$toast(res.data.info);
            // this.lastShow(); // TODO: 待续
          }
        });
    },
    sendcode() {
      let zs = this;
      if (this.phone == "") {
        this.$toast("enter you phone number");
        return;
      }
      zs.imgcodestatus = true;
    },
    loginBtnFn() {
      if (!this.btnReady) return;
      this.loginIfLoading = true;

      let zs = this;
      zs.api.userController
        .userRegitster({
          phone: zs.phoneNumber,
          msmCode: zs.phoneCode,
          inviteCode: zs.inviteCode
        })
        .then(res => {
          console.log(res);
          if (res.data.code === 200) {
            zs.loginsuccess(res.data.data); //登录信息存储
            this.$router.replace({ name: "index" });
          } else {
            zs.$toast(res.data.info);
            this.loginIfLoading = false;
          }
        });
    },
    goback() {
      this.$router.go(-1);
    },
    // 登陆成功后的数据处理
    loginsuccess(resdata) {
      let zs = this;
      let user = {
        isLogin: true,
        phone: resdata.phone, //用户名 手机号或者字符串
        avatar: resdata.avatar, //用户名 手机号或者字符串
        token: resdata.token,
        userId: resdata.userId,
        nickName: resdata.nickName,
        age: resdata.age,
        sex: resdata.sex,
        createTime: resdata.createTime,
        status: resdata.status,
        integralOne: resdata.integralOne,
        integralTwo: resdata.integralTwo,
        gainWeight: resdata.gainWeight,
        userWaltVo: resdata.userWaltVo
      };
      zs.$store.commit("saveuserdata", user);
      setTimeout(() => {
        zs.$router.push({ name: "index" });
      }, 0);
    },

    lastShow() {
      clearInterval(this.timeInterval);
      this.timeInterval = setInterval(() => {
        if (this.lastTime > 0) {
          this.lastTime--;
        } else {
          clearInterval(this.timeInterval);
          this.lastTime = this.initLastTime;
        }
      }, 1000);
    }
  },
  mounted() {
    this.phoneNumber = this.$route.query.phoneNumber;
    this.phoneShow = this.$route.query.phoneNumber;
    this.ifNew = this.$route.query.ifNew;
    this.lastTime = this.initLastTime;
    this.lastShow();
  },
  beforeDestroy() {
    clearInterval(this.timeInterval);
    this.timeInterval = null;
  }
};
</script>
<style lang="less" scoped>
@import url("./../../assets/css/common.less");

.loginwrap {
  background-color: rgba(221, 245, 255, 1);
  color: rgba(51, 51, 51, 1);
  .van-button--info {
    border-color: transparent;
  }
  box-sizing: border-box;
  .title {
    padding: 1.6rem 0.5rem 0.2rem;
    font-size: 0.4rem;

    font-weight: bold;
    line-height: 0.6rem;
    height: 0.6rem;
    color: #333333;
    opacity: 1;
  }
  .loginPicker {
    width: 6.2rem;
    height: 1rem;
    line-height: 1rem;
    background: #ffffff;
    opacity: 1;
    border-radius: 0.2rem;
    margin: 0 auto 0.3rem;
    padding: 0 0.4rem;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    .left {
      display: flex;
      //justify-content: space-between;
      p {
        padding-right: 0.4rem;
      }
    }
    .btnicon {
      height: 1rem;
      width: 1rem;
      line-height: 1rem;
      cursor: pointer;
      text-align: right;
    }
  }
  .phoneNumberShowWrap {
    width: 6.2rem;
    height: 1rem;
    line-height: 1rem;
    background: #ffffff;
    opacity: 1;
    border-radius: 0.2rem;
    margin: 0.5rem auto 0.2rem;
    padding: 0 0.4rem;
    box-sizing: border-box;
    display: flex;
    position: relative;
    * {
      height: 1rem;
      line-height: 1rem;
      padding-top: 0;
      padding-bottom: 0;
    }
    .currentCountryCode {
      width: 4em;
      text-align: center;
    }
    & > span {
    }
    .phoneNumberShow {
    }

    .codeLiWrap {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(221, 245, 255, 1);
      display: flex;
      justify-content: space-between;
      .codeLi {
        width: 0.8rem;
        height: 0.8rem;
        line-height: 0.8rem;
        text-align: center;
        color: rgba(51, 51, 51, 1);
        font-weight: bold;
        background-color: #fff;
        border-radius: 0.2rem;
      }
    }
  }
  .btns {
    .loginBtn {
      width: 6.2rem;
      height: 1rem;
      margin: 1rem auto 0.9rem;
      display: block;
      cursor: pointer;
      background: linear-gradient(
        270deg,
        rgba(254, 237, 85, 0.3),
        rgba(255, 196, 0, 0.3)
      );
      opacity: 0.3;
      border-radius: 1rem;
      font-size: 0.4rem;

      font-weight: bold;
      line-height: 1rem;
      color: #663929;
      opacity: 1;
      &.loginBtnReady {
        background: linear-gradient(
          270deg,
          rgba(254, 237, 85, 1),
          rgba(255, 196, 0, 1)
        );
        color: rgba(102, 57, 41, 1);
      }
    }
  }
  .tips {
    padding: 0 0.5rem;
    font-size: 0.24rem;

    font-weight: 400;
    line-height: 0.6rem;
    color: #666666;
    opacity: 1;
    &.tipsBtn {
      color: red;
    }
  }
}
</style>
